package com.poisonh.unifiedauth.dialog;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.lzy.okhttputils.model.HttpParams;
import com.poisonh.unifiedauth.R;
import com.poisonh.unifiedauth.model.BusinessCard;
import com.poisonh.unifiedauth.ui.MainActivity;
import com.poisonh.unifiedauth.ui.SettingCardActivity;
import com.poisonh.unifiedauth.ui.svprogresshud.SVProgressHUD;
import com.poisonh.unifiedauth.utils.ConstantUitls;
import com.poisonh.unifiedauth.utils.MyHttpsUtils;
import com.poisonh.unifiedauth.utils.ToastUtils;

import io.realm.Realm;

/**
 * 密码箱操作对话框
 * Created by PoisonH on 2016/7/6.
 */
public class CardDialogFragment extends BaseDialogFragment implements View.OnClickListener
{
    private TextView mTvCancel;
    private TextView mTvDelete;
    private TextView mTvLookEdit;


    private Activity mActivity;

    @Override
    public void onStart()
    {
        super.onStart();
        // 设置宽度为屏宽、靠近屏幕顶部。
        Window window = getDialog().getWindow();
        window.setBackgroundDrawable(new ColorDrawable(0xff000000));
        WindowManager.LayoutParams wlp = window.getAttributes();
        wlp.gravity = Gravity.BOTTOM;
        wlp.width = WindowManager.LayoutParams.MATCH_PARENT;
        wlp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(wlp);

        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        window.setLayout(dm.widthPixels, getDialog().getWindow().getAttributes().height);


    }

    @Override
    protected View loadView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        return inflater.inflate(R.layout.layout_dialog_cardedit, null);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState)
    {
        super.onViewCreated(view, savedInstanceState);
        mActivity = getActivity();
        mTvLookEdit = (TextView) view.findViewById(R.id.tv_lookedit);
        mTvDelete = (TextView) view.findViewById(R.id.tv_delete);
        mTvCancel = (TextView) view.findViewById(R.id.tv_cancel);
        mTvLookEdit.setOnClickListener(this);
        mTvDelete.setOnClickListener(this);
        mTvCancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.tv_cancel:
                this.dismiss();
                break;
            case R.id.tv_delete:
                deleteCard();
                this.dismiss();
                break;
            case R.id.tv_lookedit:
                ((CardStatuChangeEventListener)mActivity).showEditEvent();
                this.dismiss();
                break;
        }
    }

    /**
     * 删除明信片
     */
    private void deleteCard()
    {

        SVProgressHUD.showWithStatus(getActivity(), "加载中...");
        HttpParams params = new HttpParams();
      //  String mStrUserId = PreferencesUtils.getString(getActivity(), "ID");
        params.put("userId", mStrUserId);
        params.put("token", mStrUserToken);
        MyHttpsUtils.post(ConstantUitls.REQUEDT_URL + ConstantUitls.DELETEBUSINESS, params, new MyHttpsUtils.ResultCallback()
        {
            @Override
            public void onSuccess(String response)
            {

                Realm realm = Realm.getDefaultInstance();
                realm.beginTransaction();
                realm.delete(BusinessCard.class);
                realm.commitTransaction();
                realm.close();

                ToastUtils.showToast(getActivity(), "删除数据成功");
                SVProgressHUD.dismiss(mActivity);
                ((CardStatuChangeEventListener)mActivity).deleteEvent();
                dismiss();
            }

            @Override
            public void onFailure(String failReason)
            {

                ToastUtils.showToast(mActivity, "删除数据失败");
                SVProgressHUD.dismiss(mActivity);
            }
        });
    }

    public interface CardStatuChangeEventListener{
        public void  showEditEvent();
        public void  deleteEvent();
    }

}
